﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Volo.Abp;
using Volo.Abp.VirtualFileSystem;

namespace Acme.BookStore2.Controllers
{
    [RemoteService(Name = "Default")]
    [Area("bookstore2")]
    [ControllerName("DataDictionary")]
    [Route("api/bookstore2/DataDictionary")]
    public class DataDictionaryController : BookStore2Controller
    {
        private readonly IVirtualFileProvider _virtualFileProvider;

        public DataDictionaryController(IVirtualFileProvider virtualFileProvider)
        {
            _virtualFileProvider = virtualFileProvider;
        }
        
        [HttpPost]
        public Dictionary<JValue, JArray> Post([FromBody]string[] codes)
        {
            var file = _virtualFileProvider.GetFileInfo("/DataDictionary/data-dict.json");
            var dataDict = JsonConvert.DeserializeObject<dynamic>(file.ReadAsString());
            var result = new Dictionary<JValue, JArray>();
            foreach (var code in codes)
            {
                var dict = dataDict[code];
                if (dict != null)
                {
                    result.Add(dict["name"], dict["data"]);   
                }
            }
            return result;
        }
    }
}